package dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class BlogDao {
    public void insert(Blog blog){
        System.out.println("插入一个数字");
        Connection connection=null;
        PreparedStatement statement=null;
        try {
            connection= DBUtil.getConnection();
            String sql="insert into blog value(null,?,?,?,?)";
            statement=connection.prepareStatement(sql);
            statement.setString(1, blog.getTitle());
            statement.setString(2, blog.getContent());
            statement.setTimestamp(3,blog.getPostTime());
            statement.setInt(4,blog.getUserId());
            int ret=statement.executeUpdate();
            if (ret==1){
                System.out.println("插入博客成功！");
            }else {
                System.out.println("插入博客失败！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                DBUtil.close(connection,statement,null);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public List<Blog> selectAll(){
        System.out.println("查找所有博客！");
        List<Blog> blogs=new ArrayList<>();
        Connection connection=null;
        ResultSet resultSet=null;
        PreparedStatement statement=null;
        try {
            connection=DBUtil.getConnection();
            String sql="select * from blog order by postTime desc";
            statement=connection.prepareStatement(sql);
            resultSet=statement.executeQuery();
            while (resultSet.next()){
                Blog blog=new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
//                blog.setContent(resultSet.getString("content"));
                String content=resultSet.getString("content");
                if (content.length()>9){
                    content=content.substring(0,9)+"....";
                }
                blog.setContent(content);
                blog.setUserId(resultSet.getInt("userId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blogs.add(blog);
            }
            return blogs;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                DBUtil.close(connection,statement,resultSet);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    public Blog selectOne(int blogId){
        System.out.println("查找单个博客！");
        Connection connection=null;
        ResultSet resultSet=null;
        PreparedStatement statement=null;
        try {
            connection=DBUtil.getConnection();
            String sql="select * from blog where blogId=?";
            statement=connection.prepareStatement(sql);
            statement.setInt(1,blogId);
            resultSet=statement.executeQuery();
            if (resultSet.next()){
                Blog blog=new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setContent(resultSet.getString("content"));
                blog.setUserId(resultSet.getInt("userId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                return blog;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                DBUtil.close(connection,statement,resultSet);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    public void delete(int blogId){
        System.out.println("删除指定博客！");
        Connection connection=null;
        PreparedStatement statement=null;
        try {
            connection=DBUtil.getConnection();
            String sql="delete from blog where blogId=?";
            statement=connection.prepareStatement(sql);
            statement.setInt(1,blogId);
            int ret=statement.executeUpdate();
            if (ret==1){
                System.out.println("删除成功!");
            }else {
                System.out.println("删除失败！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                DBUtil.close(connection,statement,null);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return;
    }

    public static void main(String[] args) {
        BlogDao blogDao=new BlogDao();
//
        Blog blog=new Blog();
        blog.setPostTime(new Timestamp(System.currentTimeMillis()));
        blog.setUserId(2);
        blog.setTitle("5454");
        blog.setContent("4654645646545646546456465456465464564654564654645646545646546456465456465464564654564654645646545646546456465456465464564654564654645646545646546456465456");
        blogDao.insert(blog);
//        Blog blog1=blogDao.selectOne(2);
//        System.out.println(blog1);

    }
}
